package com.murphy.algorithm.likou;

/**
 * @author dongsufeng
 * @version 4.0
 * @date 2023/2/20 17:13
 */
public class LinkListMerge {

    public ListNode merge1(ListNode l1,ListNode l2){
        ListNode newList = new ListNode(0);
        ListNode temp = newList;
        while (l1!=null && l2!=null){
            if (l1.val>l2.val){
                temp.next = l2;
                temp = temp.next;
                l2 = l2.next;
            }else {
                temp.next = l1;
                temp = temp.next;
                l1 = l1.next;
            }
        }
        if (l1!=null){
            temp.next = l1;
        }else {
            temp.next=l2;
        }
        return newList;
    }
    public ListNode merge(ListNode l1,ListNode l2){
        ListNode node = new ListNode();
        ListNode temp = node;
        while (l1 != null && l2 !=null){
            if (l1.val > l2.val){
                temp.next = l2;
                l2 = l2.next;
            }else {
                temp.next = l1;
                l1 = l1.next;
            }
            temp = temp.next;
        }
        if (l1 != null){
            temp.next = l1;
        }
        if (l2 != null){
            temp.next = l2;
        }
        return node;

    }
    public ListNode merge2(ListNode l1,ListNode l2){
        ListNode node = new ListNode();
        ListNode temp = node;
        while (l1 != null){
            temp.next = l1;
            l1 = l1.next;
            temp = temp.next;
        }
        temp.next = l2;
        return node;
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        l1.next = new ListNode(2);
        l1.next.next=new ListNode(9);
        l1.next.next.next=new ListNode(12);

        ListNode l2 = new ListNode(3);
        l2.next = new ListNode(11);
//        l2.next.next=new ListNode(4);
        LinkListMerge merge = new LinkListMerge();
        ListNode newnode = merge.merge(l1,l2);
        newnode = newnode.next;
        while (newnode!=null){
            System.out.println(newnode.val);
            newnode = newnode.next;
        }
    }

    public static class ListNode{
        private int val;
        private ListNode next;
        public ListNode(){}
        public ListNode(int val){
            this.val = val;
        }
    }
}
